package com.example.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {



    public static  SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");



    private static final String[] WEEK_DAYS = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};




    /**
     * 获取Date 对象是星期几
     * @param date 日期对象
     * @return 星期几
     */
    public static String getWeekByDate(Date date){
        Calendar calendar=Calendar.getInstance();
        if(date!=null){
            calendar.setTime(date);
        }
        return WEEK_DAYS[calendar.get(Calendar.DAY_OF_WEEK)-1];
    }


    /**
     * 获取当前月份
     * @return 当前月份
     */
    public static Integer getNowMonth(){
        Calendar calendar=Calendar.getInstance();
        return calendar.get(Calendar.MONTH)+1;
    }

    /**
     * 获取当前年份
     * @return 当前年份
     */
    public static Integer getNowYear(){
        Calendar calendar=Calendar.getInstance();
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 获取date中的年份
     * @param date
     * @return
     */
    public static Integer getYearByDate(Date date){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }


    /**
     * 获取date中的月份
     * @param date
     * @return
     */
    public static Integer getMonthByDate(Date date){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH)+1;
    }


    /**
     * 获取每个月最后的一天
     * @param year
     * @param month
     * @return
     */
    public static String getDateLastDay(Integer year, Integer month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        return dft.format(cal.getTime());
    }




    /**
     * 简写  if(obj!==null && "".equals(obj))
     * @param o
     * @return  true obj 为null
     */
    public static Boolean IsEmpty(Object o){
        return o==null || o.toString().isEmpty();
    }


    /**
     * 通过毫秒数获取日期时间
     * @param time  毫秒数
     * @return 日期
     */
    public static Date getDateByLong(Long time){
        Date date=new Date();
        date.setTime(time==null?0:time);
        return date;
    }

    /**
     * 获取日期的毫秒数
     * @param date 日期
     * @return 毫秒数
     */
    public static Long getLongByDate(Date date){
        return date==null?0:date.getTime();
    }



    //获取Calender
    public static Calendar getCalender(){
        return new GregorianCalendar();
    }


    /**
     * 时间格式转换
     * @param dft
     * @param date
     * @return
     */
    public static Date getDateByFormart(SimpleDateFormat dft,Date date){
        try {
            return dft.parse(dft.format(date));
        }catch (ParseException e){
            e.getErrorOffset();
        }
        return date;
    }

    /**
     * 时间格式转成功字符串
     * @param dft
     * @param date
     * @return
     */
    public static String getStringByDate(SimpleDateFormat dft,Date date){
        return dft.format(date);
    }



    public static Date getDateByString(SimpleDateFormat dft,String str){
        try {
            return dft.parse(str);
        }catch (ParseException e){
            e.getErrorOffset();
        };
        return new Date();
    }



}
